﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using EHTWebsite.Code;
using EHTWebsiteProject.Code.Entity;

namespace EHTWebsiteProject.Code.BLL
{
    public class MenusProvider 
    {
        /// <summary>
        /// lay menu theo Role, user, type menu
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="IsAdmin"></param>
        /// <param name="TypeMenu"></param>
        /// <returns></returns>
        public static DataSet GetMenus(String UserID, Int32 IsAdmin, Int32 TypeMenu)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_GetUserMenus]";
            command.Parameters.Add(new SqlParameter("@UserID", UserID));
            command.Parameters.Add(new SqlParameter("@IsAdmin", IsAdmin));
            command.Parameters.Add(new SqlParameter("@ID", TypeMenu));
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }

        /// <summary>
        /// lay menu theo loai menu
        /// </summary>
        /// <param name="TypeMenu"></param>
        /// <returns></returns>
         public static DataSet GetMenusByType(Int32 TypeMenu)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_GetMenusByType]";

            command.Parameters.Add(new SqlParameter("@TypeMenu", TypeMenu));
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }

         /// <summary>
         /// Ham lay menu theo loai danh muc menu
         /// IsSelect = true: lay theo loai danh muc
         /// IsSelect = false: lay danh muc tru danh muc menuadmin
         /// </summary>
         /// <returns></returns>
         public static DataSet GetMenuByDanhMucMenuType(Int32 TypeMenu, Boolean IsSelect)
         {
             SqlCommand cmd = new SqlCommand();
             cmd.CommandText = "[proc_GetMeneByDanhMucMenuType]";
             cmd.CommandType = CommandType.StoredProcedure;

             cmd.Parameters.Add(new SqlParameter("@TypeMenu", TypeMenu));
             cmd.Parameters.Add(new SqlParameter("@IsSelect", IsSelect));
             return DataProvider.ExecuteDataSet(cmd);
         }

        /// <summary>
        /// lay menu theo cha
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="ParentID"></param>
        /// <returns></returns>

        public static DataSet GetMenusByDanhMucID(Int32 ID, Int32 ParentID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_GetAllMenu]";
            command.Parameters.Add(new SqlParameter("@ID", ID));
            command.Parameters.Add(new SqlParameter("@ParentID", ParentID));
            
            command.CommandType = CommandType.StoredProcedure;

            return DataProvider.ExecuteDataSet(command);
        }
        /// <summary>
        /// lay menu theo ID danh muc menu
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static DataSet GetMenusByLevel(Int32 ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_GetMenuByLevel]";           

            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ID", ID));

            return DataProvider.ExecuteDataSet(command);
        }

        /// <summary>
        /// lay menu theo menuID
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static Menus GetMenusByID(Int32 ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_GetMenuByID]";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@MenuID", ID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            Menus theMenu = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theMenu = new Menus();
                theMenu.ID = Int32.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                theMenu.MenuID = Int32.Parse(ds.Tables[0].Rows[0]["MenuID"].ToString());
                theMenu.MenuLevel = Int32.Parse(ds.Tables[0].Rows[0]["MenuLevel"].ToString());
                theMenu.MenuNameOther = ds.Tables[0].Rows[0]["MenuNameOther"].ToString();
                theMenu.MenuNameVN = ds.Tables[0].Rows[0]["MenuNameVN"].ToString();
                theMenu.MenuOrder = Int32.Parse(ds.Tables[0].Rows[0]["MenuOrder"].ToString());
                theMenu.ParentID = Int32.Parse(ds.Tables[0].Rows[0]["ParentID"].ToString());
                theMenu.IconTrangThai = ds.Tables[0].Rows[0]["IconTrangThai"].ToString();
                theMenu.ControlUrl = ds.Tables[0].Rows[0]["ControlUrl"].ToString();
                theMenu.Icon = ds.Tables[0].Rows[0]["Icon"].ToString();
                theMenu.Publish = Boolean.Parse(ds.Tables[0].Rows[0]["Publish"].ToString());
            }

            return theMenu;
        }

        /// <summary>
        /// xoa menu theo UserID
        /// </summary>
        /// <param name="UserId"></param>
        public static void DeleteMenusByUser(String UserId)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_DeleteMenubyUser]";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@UserId", UserId));
            DataProvider.ExecuteNoneQuery(command);
        }


        /// <summary>
        /// ham insert menu vao database
        /// </summary>
        /// <param name="theMenus"></param>
        /// <param name="isAdd"></param>
        /// <returns></returns>
        public static Int32 UIMenus(Menus theMenus, Boolean isAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_IUMenu]";
            command.CommandType = CommandType.StoredProcedure;

            SqlParameter theDanhMucMenusParameter = new SqlParameter();

            theDanhMucMenusParameter.ParameterName = "@MenuIDOut";
            theDanhMucMenusParameter.Direction = ParameterDirection.Output;
            theDanhMucMenusParameter.DbType = DbType.Int32;

            command.Parameters.Add(theDanhMucMenusParameter);

            command.Parameters.Add(new SqlParameter("@ID", theMenus.ID));
            command.Parameters.Add(new SqlParameter("@MenuNameOther", theMenus.MenuNameOther));
            command.Parameters.Add(new SqlParameter("@MenuID", theMenus.MenuID));
            command.Parameters.Add(new SqlParameter("@MenuNameVN", theMenus.MenuNameVN));
            command.Parameters.Add(new SqlParameter("@MenuLevel", theMenus.MenuLevel));
            command.Parameters.Add(new SqlParameter("@MenuOrder", theMenus.MenuOrder));
            command.Parameters.Add(new SqlParameter("@IconTrangThai", theMenus.IconTrangThai));
            command.Parameters.Add(new SqlParameter("@ParentID", theMenus.ParentID));
            command.Parameters.Add(new SqlParameter("@ControlUrl", theMenus.ControlUrl));
            command.Parameters.Add(new SqlParameter("@Icon", theMenus.Icon));

            command.Parameters.Add(new SqlParameter("@IsAdd", isAdd));

            DataProvider.ExecuteNoneQuery(command);

            return Int32.Parse(command.Parameters["@MenuIDOut"].Value.ToString());
        }

        /// <summary>
        /// ham thay doi trang thai cua menu
        /// </summary>
        /// <param name="MenuID"></param>
        /// <param name="IconTrangThai"></param>
        /// <param name="IsPublish"></param>
        public static void ChangePublishMenu(Int32 MenuID, string IconTrangThai, Boolean IsPublish)
        { 
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_ChangePublishByMenuID]";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@MenuID", MenuID));
            command.Parameters.Add(new SqlParameter("@IconTrangThai", IconTrangThai));
            command.Parameters.Add(new SqlParameter("@Publish", IsPublish));
            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// ham xoa menu theo menuID
        /// </summary>
        /// <param name="MenuID"></param>
        public static void DeleteMenus(Int32 MenuID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "[proc_DeleteMenu]";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@MenuID", MenuID));
            DataProvider.ExecuteNoneQuery(command);
        }

        //dungna: add, get menus products for fontend
        //
        public static DataSet GetMenusProducts()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "[proc_MenusProducts]";
            cmd.CommandType = CommandType.StoredProcedure;
            return DataProvider.ExecuteDataSet(cmd);
        }
        public static MenusProducts GetMenusProductID(Int32 DanhMucID)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "[proc_GetMenuProductID]";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@DanhMucID", DanhMucID));
            DataSet ds = DataProvider.ExecuteDataSet(cmd);
            MenusProducts theCategory = null;

            if (ds.Tables[0].Rows.Count > 0)
            {
                theCategory = new MenusProducts();
                theCategory.DeletedFlag = Boolean.Parse(ds.Tables[0].Rows[0]["DeletedFlag"].ToString());
                theCategory.TenDanhMucVN = ds.Tables[0].Rows[0]["TenDanhMucVN"].ToString();
                theCategory.TenDanhMucOther = ds.Tables[0].Rows[0]["TenDanhMucOther"].ToString();
                theCategory.DanhMucID = Int32.Parse(ds.Tables[0].Rows[0]["DanhMucID"].ToString());
                theCategory.ChuyenMucChaID = Int32.Parse(ds.Tables[0].Rows[0]["ChuyenMucChaID"].ToString());               
                theCategory.LuongTruyCap = Int32.Parse(ds.Tables[0].Rows[0]["LuongTruyCap"].ToString());
                theCategory.SapXep = Int32.Parse(ds.Tables[0].Rows[0]["SapXep"].ToString());
                theCategory.IconTrangThai = ds.Tables[0].Rows[0]["IconTrangThai"].ToString();
                theCategory.NgayTao = DateTime.Parse(ds.Tables[0].Rows[0]["NgayTao"].ToString());
                theCategory.Publish = Boolean.Parse(ds.Tables[0].Rows[0]["Publish"].ToString());

            }
            return theCategory;
        }
    }
}